package com.lanyam.travel.base.controller;

import com.lanyam.travel.base.utils.Constant;
import com.lanyam.travel.base.utils.JSONResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 版权所有： 稻草人
 * 创建者： wenhe
 * 创建时间: 2020-02-09
 * 描述：
 * 其它：
 **/
@RequestMapping(value="/upload")
@RestController
public class UploadController {

    /**
     * 上传基础信息的logo
     * @param file
     * @return
     */
    @PostMapping(value="/uploadBaseLogo")
    public JSONResult uploadBaseLogo(MultipartFile file) {
        String folderName = "baseLogo";
        JSONResult jsonResult = uploadFile(file, folderName);
        return jsonResult;
    }

    /**
     * 上传基础信息的logo
     * @param file
     * @return
     */
    @PostMapping(value="/uploadSupplyLogo")
    public JSONResult uploadSupplyLogo(MultipartFile file) {
        String folderName = "supplyLogo";
        JSONResult jsonResult = uploadFile(file, folderName);
        return jsonResult;
    }

    /**
     * 上传图片logo
     * @param files
     * @return
     */
    @PostMapping(value="/uploadRefuseImg")
    public JSONResult uploadRefuseImg(@RequestParam("file")MultipartFile[] files) {
        String folderName = "baseLogo";
        Map<Integer,Object> fileMap = new HashMap();
        int i=0;
        for(MultipartFile file:files) {
            JSONResult jsonResult1 = uploadFile(file, folderName);
            fileMap.put(i,jsonResult1.getData());
        }
        return JSONResult.ok(fileMap);
    }
    
    /**
     * 上传富文本内的图片和视频
     * @param file
     * @return
     */
    @PostMapping("/uploadRichTextPic")
    public JSONResult uploadRichTextPic(@RequestParam("file") MultipartFile file)
    {
    	String folderName = "richText";
    	JSONResult jsonResult = uploadFile(file, folderName);
    	return jsonResult;
    }

    /**
     * 展示图片
     * @param filePath
     * @param response
     */
    @GetMapping(value="/showImg")
    public void showImg(String filePath, HttpServletResponse response) {
        // 展示图片流
        filePath = Constant.FILE_UPLOAD_BASE_URI + filePath;
        File file = new File(filePath);
        OutputStream os = null;
        InputStream is = null;
        try {
            os = response.getOutputStream();
            is = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            while(is.read(bytes) > 0) {
                os.write(bytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public JSONResult uploadFile(MultipartFile file, String folderName) {
        // 生成相应从文件夹
        String folderPath = Constant.FILE_UPLOAD_BASE_URI + "/" + folderName;
        File folder = new File(folderPath);
        if(!folder.exists())
            folder.mkdirs();
        // 写入图片文件
        String filePath = "/" + folderName + "/" + UUID.randomUUID().toString() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            is = file.getInputStream();
            fos = new FileOutputStream(Constant.FILE_UPLOAD_BASE_URI + filePath);
            byte[] bytes = new byte[1024];
            while (is.read(bytes)>0) {
                fos.write(bytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Map map = new HashMap();
        map.put("filePath", filePath);
        map.put("fileName", file.getOriginalFilename());

        JSONResult ok = JSONResult.ok(map);
        return ok;
    }
}
